const { regClass } = Laya;
import { LevelQuestionInfo } from "../../../Data/LevelConfig";
import { ItemReasonRadio } from "./ItemReasonRadio";
import { PopSelectReasonBase } from "./PopSelectReason.generated";

@regClass()
export class PopSelectReason extends PopSelectReasonBase {

    private id: number;
    private arrItemRadio: ItemReasonRadio[] = [];
    private trueID = -1;
    actSelect: (a: number, b: number, c: number, d: boolean) => void;
    onAwake(): void {
        // 自身组件初始化
        this.imgMask.on(Laya.Event.CLICK, this, this.onMaskClick)

        this.arrItemRadio = [this.ItemRadioException0, this.ItemRadioException1, this.ItemRadioException2, this.ItemRadioException3];
        // // 加载单选项 
        // this.createItem(StaticData.getErrInfo());
    }
    onDestroy(): void {
        this.imgMask.off(Laya.Event.CLICK, this, this.onMaskClick)
        Laya.timer.clear(this, this.autoToSelect);
    }

    public createItem(data: LevelQuestionInfo[]) {
        let arr: string[] = [];
        for (let item of data[0].arrQuestionOptions) {
            arr.push(item);
        }
        for (let index = 0; index < this.arrItemRadio.length; index++) {
            const element = this.arrItemRadio[index];
            let item = arr.shift();
            element.setInfo(index, item);
            element.actClickMy = this.onClickRadio.bind(this);
        }

        // 重置状态
        for (let item of this.arrItemRadio) {
            item.value = false;
            item.hideErrImg();
        }
    }

    public showGuide(arr:string[]):void {        
        for (let index = 0; index < this.arrItemRadio.length; index++) {
            const element = this.arrItemRadio[index];
            let item = arr.shift();
            element.setInfo(index, item);
        }
    }



    onClickRadio(selectId: number) {
        let selectSuccess = false;
        for (let i = 0; i < this.arrItemRadio.length; i++) {
            this.arrItemRadio[i].hideErrImg();
            this.arrItemRadio[i].value = selectId == i;
            if (selectId != i) { continue; }
            // 当前选项进行选择正确错误的判断
            selectSuccess = this.trueID == selectId;
            console.log("本题选择结果", selectSuccess);

            this.arrItemRadio[i].isShowErr = !selectSuccess;
            this.actSelect?.(this.id, selectId, this.trueID, selectSuccess)
            if (!selectSuccess) { continue; }
            // 正确的选项自动隐藏
            this.hidePop()
        }
    }
    onMaskClick() {
        let selectSuccess = false;
        let seletIndex = -1;
        for (let i = 0; i < this.arrItemRadio.length; i++) {
            if (this.arrItemRadio[i].value) {
                seletIndex = i;
                selectSuccess = this.trueID == i;
                console.log("本题之前选择结果", selectSuccess);
            }
        }

        this.actSelect?.(this.id, seletIndex, this.trueID, selectSuccess)
        this.hidePop()
    }

    get popWidth() { return this.popBox.width; }
    get popHeight() { return this.popBox.height; }
    get popCenterX() { return this.popBox.centerX; }
    set popCenterX(v: number) { this.popBox.centerX = v; }
    get popCenterY() { return this.popBox.centerY; }
    set popCenterY(v: number) { this.popBox.centerY = v; }
    get popX() { return this.popBox.x }
    set popX(v: number) { this.popBox.x = v; }
    get popY() { return this.popBox.y }
    set popY(v: number) { this.popBox.y = v; }

    showPop(id: number, info: LevelQuestionInfo) {
        this.id = id;
        this.visible = true;
        this.trueID = info.questionAnswer;
        for (let item of this.arrItemRadio) {
            item.value = false;
            item.hideErrImg();
        }

        for (let index = 0; index < this.arrItemRadio.length; index++) {
            const element = this.arrItemRadio[index];
            element.setInfo(index, info.arrQuestionOptions[index]);
        }

    }
    hidePop() {
        this.visible = false;
        Laya.timer.clear(this, this.autoToSelect);
    }

    public aniToSelcet() {
        Laya.timer.clear(this, this.autoToSelect);
        Laya.timer.once(1000, this, this.autoToSelect)
    }
    private autoToSelect() {
        for (let item of this.arrItemRadio) {
            item.value = false;
            item.hideErrImg();
        }
    }
}